------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    主体框体

]]--
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 控件
    local Widget = require "widgets/widget"
    local Image = require "widgets/image"
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- image api
    local BASE_ATLAS = "images/widgets/hutao_inspect_hud.xml"
    local BUFF_ICON_ATLAS = "images/widgets/hutao_talent_buff_icon.xml"
    local function get_image(image_name)
        return TUNING.HUTAO_INSPECT_HUD_FNS.get_image(image_name)
    end
    local function get_info(index)
        return TUNING.HUTAO_INSPECT_HUD_FNS.get_info(index)
    end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 挂载函数，方便动态修改
    TUNING.HUTAO_INSPECT_HUD_FNS.create_inspect_hud = function(inst,front_root)
        ----------------------------------------------------------------------
        --- 预检查 和 模块引入
            front_root = front_root or ThePlayer.HUD
        ----------------------------------------------------------------------
        --- 基础根节点
            local base_root = front_root:AddChild(Widget())
            base_root:SetHAnchor(front_root == ThePlayer.HUD and 1 or 0) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
            base_root:SetVAnchor(front_root == ThePlayer.HUD and 2 or 0) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
            base_root:SetScaleMode(SCALEMODE_FIXEDSCREEN_NONDYNAMIC)   --- 缩放模式
        ----------------------------------------------------------------------
        --- 根节点
            local scale = 0.55
            local root = base_root:AddChild(Widget())
            root:SetScale(scale,scale,scale)
            if front_root == ThePlayer.HUD then
                root:SetPosition(700,400)                
            else
                
            end

        ----------------------------------------------------------------------
        --- 调试坐标 move_event_install
            root.front_root = front_root
        ----------------------------------------------------------------------
        --- 主背景
            local base_background = root:AddChild(Image(BASE_ATLAS,"base_background.tex"))
            base_background:SetClickable(false)
            local base_background2 = root:AddChild(Image(BASE_ATLAS,"base_background.tex"))
            base_background2:Hide()
            base_background2.inst:DoTaskInTime(0.5,function()
                base_background2:Show()
                base_background2:MoveToBack()
            end)
        ----------------------------------------------------------------------
        --- close button
            local close_button = root:AddChild(ImageButton(BASE_ATLAS,"close_button.tex","close_button.tex","close_button.tex","close_button.tex","close_button.tex"))
            close_button:SetPosition(820,390)
            close_button:SetOnClick(function()
                if front_root == ThePlayer.HUD then
                    base_root:Kill()
                else
                    front_root.inst:PushEvent("talent_tree_close")
                end
            end)
            -- move_debug_event_install(close_button,root)
        ----------------------------------------------------------------------
        --- 3个页面
            local pages_fns = {
                TUNING.HUTAO_INSPECT_HUD_FNS.create_character_page,
                TUNING.HUTAO_INSPECT_HUD_FNS.create_skill_page,
                TUNING.HUTAO_INSPECT_HUD_FNS.create_ascension_page,
            }
            for k, fn in pairs(pages_fns) do
                local flag,crash_reason = pcall(fn,inst,root)
                if flag == false then
                    print("hutao import inspect page error",crash_reason)
                end
            end
        ----------------------------------------------------------------------
        --- 3个按钮。
            local page_button_offset = Vector3(7,0,0)
            local page_button_data = {}
            page_button_data[1] = {}
            page_button_data[1].pt = Vector3(800,210,0)+page_button_offset
            page_button_data[1].event = "display_character_page"
            page_button_data[1].selected = {}
            page_button_data[1].selected.atlas ,page_button_data[1].selected.image = get_image("button_character")
            page_button_data[1].unselected = {}
            page_button_data[1].unselected.atlas ,page_button_data[1].unselected.image = get_image("button_character_unselected")
            page_button_data[2] = {}
            page_button_data[2].pt = Vector3(800,70,0)+page_button_offset
            page_button_data[2].event = "display_skill_page"
            page_button_data[2].selected = {}
            page_button_data[2].selected.atlas ,page_button_data[2].selected.image = get_image("button_skill")
            page_button_data[2].unselected = {}
            page_button_data[2].unselected.atlas ,page_button_data[2].unselected.image = get_image("button_skill_unselected")
            page_button_data[3] = {}
            page_button_data[3].pt = Vector3(800,-70,0)+page_button_offset
            page_button_data[3].event = "display_ascension_page"
            page_button_data[3].selected = {}
            page_button_data[3].selected.atlas ,page_button_data[3].selected.image = get_image("button_ascension")
            page_button_data[3].unselected = {}
            page_button_data[3].unselected.atlas ,page_button_data[3].unselected.image = get_image("button_ascension_unselected")


            local page_buttons = {}
            for index, data in pairs(page_button_data) do
                local temp_button = root:AddChild(ImageButton(
                    data.unselected.atlas,
                    data.unselected.image,
                    data.unselected.image,
                    data.unselected.image,
                    data.unselected.image,
                    data.unselected.image
                ))
                temp_button:SetPosition(data.pt.x,data.pt.y)
                temp_button.inst:ListenForEvent("on_click",function()
                    -- root.inst:PushEvent(data.event) --- 切换页面
                    root.inst:PushEvent("page_display",data.event) --- 通过事件推送，进行界面显示/隐藏。
                    for index, button in pairs(page_buttons) do
                        if button == temp_button then
                            button.inst:PushEvent("selected")
                        else
                            button.inst:PushEvent("unselected")
                        end
                    end
                end)
                temp_button:SetOnClick(function()
                    temp_button.inst:PushEvent("on_click")
                end)
                temp_button.inst:ListenForEvent("selected",function()
                    temp_button:SetTextures(data.selected.atlas,data.selected.image,data.selected.image,data.selected.image,data.selected.image,data.selected.image)
                end)
                temp_button.inst:ListenForEvent("unselected",function()
                    temp_button:SetTextures(data.unselected.atlas,data.unselected.image,data.unselected.image,data.unselected.image,data.unselected.image,data.unselected.image)
                end)
                temp_button.focus_scale = {1.05, 1.05, 1.05}
                table.insert(page_buttons,temp_button)
                temp_button:MoveToBack()
            end                    
        ----------------------------------------------------------------------
        ---- 缺省显示
            local default_display_index = TUNING.HUTAO_INSPECT_HUD_FNS.default_display_index or 1
            page_buttons[default_display_index].inst:PushEvent("on_click")
        ----------------------------------------------------------------------
        --- 调试
            if TUNING.HUTAO_DEBUGGING_MODE then
                local temp_handler = TheInput:AddKeyHandler(function(key,down)
                    if down and key == 96 then
                        base_root:Kill()
                    end
                end)
                base_root.inst:ListenForEvent("onremove",function()
                    temp_handler:Remove()
                end)
            end
        ----------------------------------------------------------------------
        ---
            return base_root
        ----------------------------------------------------------------------
    end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AddPrefabPostInit(
    "hutao",
    function(inst)
        inst:DoTaskInTime(0,function()
            if ThePlayer and ThePlayer == inst and ThePlayer.HUD then
                inst:ListenForEvent("hutao_event.client_side_inspect_hud_open",function(inst,widget_root)
                    TUNING.HUTAO_INSPECT_HUD_FNS.create_inspect_hud(inst,widget_root or inst.HUD)
                end)
            end
        end)

    end
)
